<script setup lang="ts">
import { ref } from 'vue'
import { useCreateCompany } from '../composables/useCreateCompany'
import { baseUrl } from '../config'

const emit = defineEmits(['created'])
const name = ref('')
const comment = ref('')
const { createCompany, loading, error, result } = useCreateCompany(baseUrl)

async function handleSubmit() {
  if (!name.value) return
  await createCompany({ name: name.value, comment: comment.value })
  if (result.value && result.value.id !== undefined) {
    emit('created')
    name.value = ''
    comment.value = ''
  }
}
</script>

<template>
  <div class="create-company-panel">
    <h3>创建组织</h3>
    <form @submit.prevent="handleSubmit">
      <div class="form-item">
        <label>组织名称：</label>
        <input v-model="name" required placeholder="请输入组织名称" />
      </div>
      <div class="form-item">
        <label>组织说明：</label>
        <input v-model="comment" placeholder="请输入组织说明" />
      </div>
      <button type="submit" :disabled="loading">创建</button>
    </form>
    <div v-if="error" class="error">{{ error }}</div>
    <div v-if="result" class="success">创建成功，ID: {{ result.id }}</div>
  </div>
</template>

<style scoped>
.create-company-panel {
  max-width: 400px;
  margin: 32px auto;
  padding: 24px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}
.form-item {
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
}
label {
  margin-bottom: 4px;
  font-weight: bold;
}
input {
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #ddd;
}
button {
  padding: 8px 24px;
  border: none;
  border-radius: 6px;
  background: #1890ff;
  color: #fff;
  font-size: 16px;
  cursor: pointer;
}
button:disabled {
  background: #bcdffb;
  cursor: not-allowed;
}
.error {
  color: #ff4d4f;
  margin-top: 8px;
}
.success {
  color: #52c41a;
  margin-top: 8px;
}
</style>
